package com.cwt_taxi_xa;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.MyStatus;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.service.SerialPortService;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.SPUtils;
import com.cwt_taxi_xa.util.Tools;

public class ActivityFMConfig extends CWTBaseActivity implements SeekBar.OnSeekBarChangeListener , View.OnClickListener{

	final static String TAG = "ActivityFMConfig";

	SeekBar sbAcp,sbSMeter,sbDetuning,sbMultipath,sbScalingthrmin,sbScalingthrmax,sbFrequency;
	TextView txAcpValue,txSMeter,txDetuning,txMultipath,txFsScalingthrmin,txFsScalingthrmax,txFrequency;
	Handler mHandler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		openSwipeBack(true);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fm_config);
		AppLog.d(TAG, "onCreate");

		sbAcp = (SeekBar)findViewById(R.id.sb_acp);
		sbSMeter = (SeekBar)findViewById(R.id.sb_s_meter);
		sbDetuning = (SeekBar)findViewById(R.id.sb_detuning);
		sbMultipath = (SeekBar)findViewById(R.id.sb_multipath);
		sbScalingthrmin = (SeekBar)findViewById(R.id.sb_fs_scalingthrmin);
		sbScalingthrmax = (SeekBar)findViewById(R.id.sb_fs_scalingthrmax);

		sbFrequency = (SeekBar) findViewById(R.id.sb_frequency);

		txAcpValue = (TextView)findViewById(R.id.tv_acp_value);
		txSMeter = (TextView)findViewById(R.id.tv_s_meter);
		txDetuning = (TextView)findViewById(R.id.tv_detuning);
		txMultipath = (TextView)findViewById(R.id.tv_multipath);
		txFsScalingthrmin = (TextView)findViewById(R.id.tv_fs_scalingthrmin);
		txFsScalingthrmax = (TextView)findViewById(R.id.tv_fs_scalingthrmax);
		txFrequency = (TextView)findViewById(R.id.tv_frequency);

		findViewById(R.id.btn_frequency_1).setOnClickListener(this);
		findViewById(R.id.btn_frequency_2).setOnClickListener(this);
		findViewById(R.id.btn_frequency_3).setOnClickListener(this);
		findViewById(R.id.btn_open_fm).setOnClickListener(this);
		findViewById(R.id.btn_close_fm).setOnClickListener(this);

		sbAcp.setOnSeekBarChangeListener(this);
		sbSMeter.setOnSeekBarChangeListener(this);
		sbDetuning.setOnSeekBarChangeListener(this);
		sbMultipath.setOnSeekBarChangeListener(this);
		sbScalingthrmin.setOnSeekBarChangeListener(this);
		sbScalingthrmax.setOnSeekBarChangeListener(this);

		sbAcp.setMax(20);
		sbSMeter.setMax(60);
		sbDetuning.setMax(12);
		sbMultipath.setMax(30);
		sbScalingthrmin.setMax(60);
		sbScalingthrmax.setMax(60);

		sbAcp.setProgress(SPUtils.getParamInteger("fm_sbAcp",10));
		sbSMeter.setProgress(SPUtils.getParamInteger("fm_sbSMeter",30));
		sbDetuning.setProgress(SPUtils.getParamInteger("fm_sbDetuning",6));
		sbMultipath.setProgress(SPUtils.getParamInteger("fm_sbMultipath",15));
		sbScalingthrmin.setProgress(SPUtils.getParamInteger("fm_sbScalingthrmin",30));
		sbScalingthrmax.setProgress(SPUtils.getParamInteger("fm_sbScalingthrmax",30));

		sbFrequency.setMax(205);
		sbFrequency.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
				if(fromUser){
					int value = 87500 + seekBar.getProgress() * 100;
					try {
						String ddd = Tools.parseByte2HexStr(Tools.int2Bytes2(value,4));
						AppLog.d(TAG,"sbFrequency:" + ddd + " value:" + value);
						setFM(ddd);
					} catch (Exception e) {
						e.printStackTrace();
					}

				}
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {

			}

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {

			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AppLog.d(TAG,"onResume");
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		MCUUtil.closeFM();
		MCUUtil.closeFMSearch();
		MCUUtil.selecterVic(2);
	}

	@Override
	public void setStatus(int index, boolean status) {

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub
		AppLog.d(TAG,"sendMsg type:" + type);
		if(type==790){
			AppLog.d(TAG,"sendMsg 1002");
			myToast("请测试频点");
		}
	}


	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

		switch (seekBar.getId()){
			case R.id.sb_acp:
				txAcpValue.setText(String.valueOf(56 + progress));
				break;
			case R.id.sb_s_meter:
				txSMeter.setText(String.valueOf(156 + progress));
				break;
			case R.id.sb_detuning:
				txDetuning.setText(String.valueOf(progress));
				break;
			case R.id.sb_multipath:
				txMultipath.setText(String.valueOf(progress));
				break;
			case R.id.sb_fs_scalingthrmin:
				txFsScalingthrmin.setText(String.valueOf(150 + progress));
				break;
			case R.id.sb_fs_scalingthrmax:
				txFsScalingthrmax.setText(String.valueOf(165 + progress));
				break;
		}
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {

	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		switch (seekBar.getId()){
			case R.id.sb_acp:
				SPUtils.setParam("fm_sbAcp",seekBar.getProgress());
				break;
			case R.id.sb_s_meter:
				SPUtils.setParam("fm_sbSMeter",seekBar.getProgress());
				break;
			case R.id.sb_detuning:
				SPUtils.setParam("fm_sbDetuning",seekBar.getProgress());
				break;
			case R.id.sb_multipath:
				SPUtils.setParam("fm_sbMultipath",seekBar.getProgress());
				break;
			case R.id.sb_fs_scalingthrmin:
				SPUtils.setParam("fm_sbScalingthrmin",seekBar.getProgress());
				break;
			case R.id.sb_fs_scalingthrmax:
				SPUtils.setParam("fm_sbScalingthrmax",seekBar.getProgress());
				break;
		}
		changeFm();
	}

	private void changeFm(){

		int acp = 56 + sbAcp.getProgress();
		int sMeter = 156 + sbSMeter.getProgress();
		int detuning = 0 + sbDetuning.getProgress();
		int multipath = 0 + sbMultipath.getProgress();
		int scalingthrmin = 150 + sbScalingthrmin.getProgress();
		int scalingthrmax = 165 + sbScalingthrmax.getProgress();


		txAcpValue.setText(String.valueOf(acp));
		txSMeter.setText(String.valueOf(sMeter));
		txDetuning.setText(String.valueOf(detuning));
		txMultipath.setText(String.valueOf(multipath));
		txFsScalingthrmin.setText(String.valueOf(scalingthrmin));
		txFsScalingthrmax.setText(String.valueOf(scalingthrmax));
		byte[]  dataBuffer = new byte[]{(byte)acp,(byte)sMeter,(byte)detuning,(byte)multipath,(byte)scalingthrmin,(byte)scalingthrmax};
		SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.parseHexStr2Byte("55 61 0008 "+ Tools.parseByte2HexStr(dataBuffer))));
	}

	void setFM(final String pl) {
		//0x7a 0x33 00 06 0x00,0x01,0x86,0x0a xor 0x7a

		AppLog.d(TAG, "setFM pl:" + pl);
		if (MyStatus.vichl != 1) {
			MCUUtil.selecterVic(1);
		}
		new Thread(new Runnable() {
			@Override
			public void run() {
				SerialPortService.getInstance().sendSerialData7(MCUUtil.getMCUData(Tools.
						parseHexStr2Byte("55 33 0006" + pl)));
			}
		}).start();
		double db = Integer.parseInt(pl, 16);
		db = db/1000;
		txFrequency.setText(""+db);
	}

	@Override
	public void onClick(View v) {
		String frequency = "";
		switch (v.getId()){
			case R.id.btn_frequency_1:
				frequency = Tools.parseByte2HexStr(Tools.int2Bytes2(90100,4));
				break;
			case R.id.btn_frequency_2:
				frequency = Tools.parseByte2HexStr(Tools.int2Bytes2(98500,4));
				break;
			case R.id.btn_frequency_3:
				frequency = Tools.parseByte2HexStr(Tools.int2Bytes2(106500,4));
				break;
			case R.id.btn_open_fm:
				MCUUtil.openFM();
				return;
			case R.id.btn_close_fm:
				MCUUtil.closeFM();
				MCUUtil.closeFMSearch();
				MCUUtil.selecterVic(2);
				return;
		}
		if(!TextUtils.isEmpty(frequency)){
			setFM(frequency);
		}
	}
}
